using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Diagnostics;
using System.Windows.Forms;

namespace Janus.NorthwindDemo2007
{
	public partial class CustomerDataForm
	{

		protected override void StartEdition()
		{
			this.CustomerControl1.UseTabBackground = true;
			this.CustomerControl1.DataSource = this.DataSet;
			if (this.EditMode == DataFormEditMode.AddNew)
			{
				this.Text = "Customers - New Customer";
				this.CustomerControl1.IsAddNew = true;
			}
			else
			{
				this.Text = "Customers - " + ((DataRowView)CurrencyManager.Current)["CompanyName"];
				this.CustomerControl1.IsAddNew = false;
			}
			this.CustomerControl1.Select();
		}


		private bool mOrdersFilled;
		private bool mProductsFilled;
		private void FillOrders()
		{
			if (! mOrdersFilled)
			{
				if (! this.DataRowView.IsNew)
				{
					this.ordersTableAdapter.Fill((String)this.DataRowView["CustomerID"], this.DataSet.OrdersExtended);
				}
				mOrdersFilled = true;
			}
		}
		private void FillProducts()
		{
			if (! mProductsFilled)
			{
				if (! this.DataRowView.IsNew)
				{
					this.jsgProducts.RootTable.Columns["ProductID"].HasValueList = true;
					this.jsgProducts.RootTable.Columns["ProductID"].ValueList.PopulateValueList(NorthwindApp.NorthwindApplication.GetProductList().DefaultView, "ProductID", "ProductName");
					this.orderDetailsAdapter.FillByCustomer((string)this.DataRowView["CustomerID"], this.DataSet.OrderDetailsExtended);
				}
				this.jsgProducts.CollapseGroups();
				this.jsgProducts.Row = 0;
				mProductsFilled = true;
			}
		}



		private void UiTab1_SelectedTabChanged(object sender, Janus.Windows.UI.Tab.TabEventArgs e)
		{
			if (e.Page == this.tabOrders)
			{
				this.FillOrders();
				this.lblNoOrders.Visible = (this.DataSet.OrdersExtended.Count == 0);
			}
			else if (e.Page == this.tabProducts)
			{
				this.FillProducts();
			}
		}

		private void jsgProducts_LoadingRow(object sender, Janus.Windows.GridEX.RowLoadEventArgs e)
		{
			if (e.Row.RowType == Windows.GridEX.RowType.Record)
			{
				//calculate the amount
				decimal unitPrice = 0M;
				short quantity = 0;
				float discount = 0F;

				if (! System.Convert.IsDBNull(e.Row.Cells["UnitPrice"].Value))
				{
					unitPrice = System.Convert.ToDecimal(e.Row.Cells["UnitPrice"].Value);
				}
				if (! System.Convert.IsDBNull(e.Row.Cells["Quantity"].Value))
				{
					quantity = System.Convert.ToInt16(e.Row.Cells["Quantity"].Value);
				}
				if (! System.Convert.IsDBNull(e.Row.Cells["Discount"].Value))
				{
					discount = System.Convert.ToSingle(e.Row.Cells["Discount"].Value);
				}
				e.Row.Cells["Amount"].Value = (float)unitPrice * quantity * (1 - discount);
			}
		}
	}

} //end of root namespace